<?php


/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

class HelpDeskHelperTicket 
{
	/**
	 * Returns an object
	 * @param mixed Boolean
	 * @return array
	 */
	function getInstance( $id='0' ) 
	{
		$id = intval($id);
		HelpDeskTable::addIncludePath( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_helpdesk'.DS.'tables'.DS );
		unset($row);
		$row = HelpDeskTable::getInstance( 'Ticket', 'Table' );
		$row->load( $id );
		
		return $row;
	}
	
	/**
	 * 
	 * @param $id
	 * @param $userid
	 * @return unknown_type
	 */
	function canView( $id, $userid )
	{
		$database = &HelpDeskConfig::getDBO();
		$success = false;

		$where = array();
		$lists = array();
		
		$ticket_query = " AND t.id = '".$id."' ";
	
		// all records		
		$query = "SELECT t.* FROM #__helpdesk_tickets AS t "
		. " WHERE 1 "
		. $ticket_query
		;

		$database->setQuery( $query );
		$data = $database->loadObject();
		
		// if ticket associated with userid
		if ($data->sender_userid == $userid) { $success = true; }
		// or if userid is a manager of ticket category/section
		$manager = &HelpDeskHelperHelpDesk::isCategoryManager( $data->categoryid, $userid );
		if ($manager) { $success = true; }
		// or if superadmin
		$user = &HelpDeskConfig::getUser( $userid );
		if ($user->gid == '25') { $success = true; }		
		
		return $success;
	}
	
	/**
	 * 
	 * @param $id
	 * @param $userid
	 * @param $fileid
	 * @return unknown_type
	 */
	function canViewAttachment( $id, $userid, $fileid )
	{
		$database = &HelpDeskConfig::getDBO();
		$success = false;

		$where = array();
		$lists = array();
		
		$file_query = " AND f.id = '".$fileid."' ";
	
		// all records		
		$query = "SELECT f.* FROM #__helpdesk_files AS f "
		. " WHERE 1 "
		. $file_query
		;

		$database->setQuery( $query );
		if (!$data = $database->loadObject() ) { return $success; }
		
		// if file not assoc with ticket
		if ($id != $data->ticketid) { return $success; }
		
		if ($canView = &HelpDeskHelperHelpDesk::canViewTicket( $id, $userid )) { $success = true; }
		
		return $success;
	}
	
	/**
	 * 
	 * @param $id
	 * @return unknown_type
	 */
	function getMessages( $id )
	{
		$success = false;
		$database = &HelpDeskConfig::getDBO();
		
		$query = "
			SELECT 
				msg.*, user.username, user.name, user.email 
			FROM 
				#__helpdesk_messages AS msg
			LEFT JOIN 
				#__users AS user on msg.userid_from = user.id
			WHERE 1
				AND msg.ticketid = '".intval($id)."'
			ORDER BY 
				msg.datetime DESC
		";
		$database->setQuery( $query );
		$success = $database->loadObjectList();

		return $success;
	}
	
	/**
	 * Returns a list
	 * @param mixed Boolean
	 * @param mixed Boolean
	 * @return array
	 */
	function getAdditionalData( $id ) 
	{
		$database = &HelpDeskConfig::getDBO();

		$where = array();
		$lists = array();

		$ticket_query = " AND c.ticketid = '".intval($id)."' ";
		
		$query = "
			SELECT 
				c.* 
			FROM 
				#__helpdesk_ticketdata AS c
			WHERE 1 
				{$ticket_query}
		";

		$database->setQuery( $query );
		$data = $database->loadObject();
		
		return $data;
	}
	
	/**
	 * 
	 * @param $id
	 * @return unknown_type
	 */
	function getAttachments( $id )
	{
		$database = &HelpDeskConfig::getDBO();

		$where = array();
		$lists = array();

		$ticket_query = " AND t.ticketid = '".$id."' ";
	
		// all records		
		$query = "SELECT t.* FROM #__helpdesk_files AS t "
		. " WHERE 1 "
		. $ticket_query
		;

		$database->setQuery( $query );
		$data = $database->loadObjectList();
		return $data;
	}
	
	/**
	 * Returns a ticket status
	 * @param mixed Boolean
	 * @return array
	 */
	function getStatus( $id ) 
	{
		$type = HelpDeskHelperStatus::getType( $id, 'id' );
		$data = HelpDeskText::_( $type->title );
		return $data;
	}
}